package com.rustic.server.controller.api;

import cn.xy.commons.annotation.ApiAuth;
import cn.xy.commons.annotation.RepeatSubmit;
import cn.xy.commons.constant.Consts;
import cn.xy.commons.core.Result;
import cn.xy.commons.exception.JzRuntimeException;
import cn.xy.third.sms.enums.CaptchaType;
import com.rustic.server.dto.user.BindMobileCaptchaDTO;
import com.rustic.server.dto.user.CheckCaptchaDTO;
import com.rustic.server.dto.user.EmailDTO;
import com.rustic.server.dto.user.MobileDTO;
import com.rustic.server.dto.user.UserUpdatePasswordDTO;
import com.rustic.server.dto.user.UpdatePersonalDTO;
import com.rustic.server.po.User;
import com.rustic.server.service.CaptchaService;
import com.rustic.server.service.UserService;
import com.rustic.server.vo.user.UserSimpleVO;
import com.rustic.server.web.WebContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author auto
 */

@Slf4j
@RestController
@RequestMapping("/api/user")
@Api(tags = {"用户接口"})
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private CaptchaService captchaService;

    @ApiOperation(value = "用户-详情")
    @GetMapping("/info")
    public Result<UserSimpleVO> info() {
        int userId = WebContext.getUserId();
        if (userId == 0) {
            return Result.error(Consts.NO_LOGIN_CODE, "登录信息已失效，请重新登录");
        }
        UserSimpleVO userVO = userService.getSimpleById(userId);
        if (userVO == null) {
            return Result.error(UserService.USER_NOT_EXIST);
        }
        return Result.data(userVO);
    }

    @ApiOperation(value = "验证码修改密码")
    @PostMapping("/updatePassword")
    @ApiAuth(ignore = true)
    @RepeatSubmit
    public Result<String> updatePassword(@RequestBody @Validated UserUpdatePasswordDTO dto) {
        // 检验验证码
        captchaService.checkCaptcha(CaptchaType.PASSWORD, dto.getCaptcha(), dto.getAccount());

        User user = userService.getByMatch(dto.getAccount());
        if (user == null) {
            return Result.error(UserService.USER_NOT_EXIST);
        }
        userService.updatePassword(user.getId(), dto.getPassword());
        return Result.ok("密码修改成功！");
    }

    @ApiOperation(value = "发送密码重置验证码(短信)")
    @PostMapping("/sendResetPasswordCaptcha")
    @ApiAuth(ignore = true)
    public Result<String> sendResetPasswordCaptcha(@RequestBody @Validated MobileDTO mobileDTO) {
        User user = userService.getByMobile(mobileDTO.getMobile());
        if (user == null) {
            throw new JzRuntimeException("用户不存在");
        }
        captchaService.sendSmsCaptcha(CaptchaType.PASSWORD, mobileDTO.getMobile());
        return Result.ok();
    }

    @ApiOperation(value = "发送密码重置验证码(邮件)")
    @PostMapping("/sendResetPasswordCaptchaByEmail")
    @ApiAuth(ignore = true)
    public Result<String> sendResetPasswordCaptchaByEmail(@RequestBody @Validated EmailDTO mobileDTO) {
        // TODO 邮件验证码
//        User user = userService.getByEmail(mobileDTO.getEmail());
//        if (user == null) {
//            throw new JzRuntimeException("用户不存在");
//        }
        captchaService.sendEmailCaptcha(CaptchaType.PASSWORD, mobileDTO.getEmail());
        return Result.ok();
    }

    @ApiOperation(value = "检验密码重置的验证码")
    @PostMapping("/checkResetPasswordCaptcha")
    @ApiAuth(ignore = true)
    @RepeatSubmit
    public Result<String> checkResetPasswordCaptcha(@RequestBody @Validated CheckCaptchaDTO checkCaptchaDTO) {
        captchaService.checkCaptcha(CaptchaType.PASSWORD, checkCaptchaDTO.getCaptcha(), checkCaptchaDTO.getAccount());
        return Result.ok();
    }

    @ApiOperation(value = "获取手机绑定验证码")
    @PostMapping("/sendBindMobileCaptcha")
    public Result<String> sendBindMobileCaptcha(@RequestBody @Validated MobileDTO mobileDTO) {
        User user = userService.getByMobile(mobileDTO.getMobile());
        if (user != null) {
            if (user.getId() == WebContext.getUserId()) {
                throw new JzRuntimeException("该手机号已绑定当前账户");
            } else {
                throw new JzRuntimeException("该手机号已绑定其他账户");
            }
        }
        captchaService.sendSmsCaptcha(CaptchaType.BIND_ACCOUNT, mobileDTO.getMobile());
        return Result.ok();
    }

    @ApiOperation(value = "手机绑定")
    @PostMapping("/bindMobile")
    @RepeatSubmit
    public Result<String> bindMobile(@RequestBody @Validated BindMobileCaptchaDTO dto) {
        captchaService.checkCaptcha(CaptchaType.BIND_ACCOUNT, dto.getCaptcha(), dto.getMobile());
        userService.bindMobile(WebContext.getUserId(), dto.getMobile());
        return Result.ok();
    }

    @ApiOperation(value = "修改个人资料")
    @PostMapping("/updatePersonalData")
    @RepeatSubmit
    public Result<String> updatePersonalData(@RequestBody @Validated UpdatePersonalDTO dto) {
        int userId = WebContext.getUserId();
        userService.updateUserToWeb(dto, userId);
        return Result.ok();
    }

}
